<?php
/**
 * 前台用户 控制器
 * 
 */
class EuserAction extends Action
{
	/**
	 * 网站统一配置
	 *
	 * @var unknown_type
	 */
	public $Config;
	
	/**
	 * 构造函数
	 *
	 */
	public function _initialize()
	{
		$Config = D('Config');
		$this->Config = $Config->find('config_id=1');
	}
    public function mode2(){
		import('ORG.Util.Image');
		Image::buildImageVerify(6,0,'jpeg',60,22);
	}
	/**
	 * 普通用户管理首页
	 *
	 */
	public function GeneralIndex()
	{
		EasyACL('Euser','GeneralIndex',$_SESSION);
        //获取友情链接
        $link = D('Friendlinks');
        $Friendlinks1 = $link->where('cid=1')->order('links_order desc,fid desc')->limit(18)->findall();
        $Friendlinks2 = $link->where('cid=2')->order('links_order desc,fid desc')->limit(18)->findall();
        
        $this->assign('Friendlinks1',$Friendlinks1);
        $this->assign('Friendlinks2',$Friendlinks2);
        
		$this->assign('Config',$this->Config);
		$this->display();
	}
	/**
	 * VIP用户首页
	 */
	public function VipIndex()
	{
		EasyACL('Euser','VipIndex',$_SESSION);
        //获取友情链接
        $link = D('Friendlinks');
        $Friendlinks1 = $link->where('cid=1')->order('links_order desc,fid desc')->limit(18)->findall();
        $Friendlinks2 = $link->where('cid=2')->order('links_order desc,fid desc')->limit(18)->findall();
        
        $this->assign('Friendlinks1',$Friendlinks1);
        $this->assign('Friendlinks2',$Friendlinks2);
		$this->assign('Config',$this->Config);
		$this->display();
	}
	/**
	 * 用户登陆页面
	 */
	public function Login()
	{
		//POST 如果是POST提交则执行POST
		if ($this->isPost()) {
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			
			import('ORG.Util.HashMap');
			//设置查询条件
			$conditions = new HashMap();
			$conditions->put('username',array('EQ',$Input->post('username')));
			$conditions->put('password',array('EQ',md5($Input->post('password'))));
			
			$User = D('Euser');
			$AllUser = $User->where($conditions)->xfind();
			
			//如果登录成功
			if ($AllUser >= 1) {
				//判断账号是否锁定?
				if ($AllUser['lock'] == 'N') {
					//注册SESSION
					$_SESSION['euser_id'] = $AllUser['euser_id']; //用户编号
					$_SESSION['username'] = $AllUser['username']; //注册用户名
					$_SESSION['Q'] = $AllUser['q']; //注册权限
					$_SESSION['enter_id'] = $AllUser['Enterprise']['enter_id'];
					$_SESSION['videolock'] = $AllUser['Enterprise']['videolock'];
					//注册成功后，进行跳转
					//如果是普通用户则跳转到普通用户首页,是否跳转到VIP用户首页
					if ($AllUser['q'] == 'vip0') {
						return $this->redirect('VipIndex','Euser');
					} else {
						return $this->redirect('VipIndex','Euser');
					}
				} else {
					//如果锁定
					return $this->error('您的账号已被锁定，请联系管理员进行解锁');
				}
			} else {
				return $this->error('对不起，您输入的账号不正确! 请检查后重新输入');
			}
			
		}
		$this->assign('Config',$this->Config);
		//显示用户登陆页面
		$this->display();
	}
	/**
	 * 账号申请
	 * 
	 */
	public function Regedit()
	{
		//POST
		if ($this->isPost()){
			$User = D('Euser');
			if ($User->create()) {
				if ($User->add()) {
					$this->assign('Message','注册成功!');
					return $this->display('Success');
				} else {
					return $this->error($User->getError());
				}
			} else {
				return $this->error($User->getError());
			}
			exit();
		}
		$this->assign('Config',$this->Config);
		//显示注册页面
		return $this->display();
	}
	
	/**
	 * 账号资料修改
	 */
	public function Update()
	{
		EasyACL('Euser','Update',$_SESSION);
		$User = D('Euser');
		
		import('ORG.Util.HashMap');
		$Conditions = new HashMap();
		$Conditions->put('euser_id',array('EQ',$_SESSION['euser_id']));
		
		if ($this->isPost()) {
			$User->where($Conditions)->find();
			if ($User->create()) {
				if ($User->save()) {
					$this->success('资料修改成功!');
				}
			} else {
				return $this->error('尝试修改资料失败! 验证失败');
			}
		}
		
		//获取要修改的账号信息
		$UserInfomation = $User->where($Conditions)->find();
		
		//To templates
		$this->assign('Config',$this->Config);
		$this->assign('User',$UserInfomation);
		$this->display();
	}
	
	/**
	 * 账号密码修改
	 */
	public function UpdatePassword()
	{
		EasyACL('Euser','UpdatePassword',$_SESSION);
		//POST
		if ($this->isPost()) {
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			
			$User = D('Euser');
			import('ORG.Util.HashMap');
			$conditions = new HashMap();
			$conditions->put('euser_id',array('EQ',$_SESSION['euser_id']));
			$conditions->put('username',array('EQ',$_SESSION['username']));
			$conditions->put('password',array('EQ',md5($_POST['pwd'])));
			//计算符合条件的账号
			$count = $User->count($conditions,'euser_id');
			if ($count > 0) {
				$User->where($conditions)->find();
				$User->password = md5($Input->post('password'));
				if ($User->save()) {
					return $this->success('密码修改成功!');
				} else {
					return $this->error('密码修改失败!');
				}
			} else {
				return $this->error('请检查您输入的原始密码是否正确!');
			}
		}
		//设置到模板
		$this->assign('Config',$this->Config);
		$this->display();
	}
    /**
     * 退出登陆
     */
    public function UserLoginOut()
    {
    	session_destroy();
    	$this->redirect('Show','Index');
    }
}
?>